package com.talking.view;

import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.talking.R;
import com.talking.databinding.ActivityImageBinding;
import com.talking.utils.Utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class ImageActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        @NonNull ActivityImageBinding binding = ActivityImageBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        String[] PERMISSIONS = {"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE" };
        int permission = ContextCompat.checkSelfPermission(this, "android.permission.WRITE_EXTERNAL_STORAGE");
        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, PERMISSIONS,1);
        }
        Glide.with(this).load(getIntent().getStringExtra("img")).apply(Utils.getGlideOption()).placeholder(R.drawable.loading).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(binding.imageImage);
        binding.imageImage.setOnLongClickListener(v -> {
            AlertDialog.Builder dialog=new AlertDialog.Builder(ImageActivity.this);
            dialog.setTitle("提示").setMessage("是否保存图片到手机相册？").setPositiveButton("确定", (dialog1, which) -> {
                new Handler().postDelayed(() -> {
                    try {
                        URLConnection conn = new URL(getIntent().getStringExtra("img")).openConnection();
                        conn.connect();
                        InputStream is = conn.getInputStream();
                        Bitmap bm = BitmapFactory.decodeStream(is);
                        is.close();
                        File dir = new File(Environment.getExternalStorageDirectory().getPath()+"/DCIM/talking/");
                        if (!dir.exists())dir.mkdirs();
                        FileOutputStream saveImgOut = new FileOutputStream(new File(dir.getAbsolutePath(),System.currentTimeMillis()+".png"));
                        bm.compress(Bitmap.CompressFormat.JPEG, 80, saveImgOut);
                        saveImgOut.flush();
                        saveImgOut.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    runOnUiThread(() ->Utils.toast( "已保存"));
                },0);
            }).setNeutralButton("取消",null).show();
            return true;
        });
    }
}
